#include "SWM341.h"
//#include "MYLIB.h"
//#include "st7789.h"

#include "main.h"

#define LCD_W 360
#define LCD_H 360

uint16_t BACK_COLOR; //背景色

void delay_ms(uint16_t ms)
{
	while(ms)
	{
		ms--;
		for(uint32_t i = 0; i < 8000; i++);
	}
}


void st7789_write_data8(uint8_t data)
{
    ST7789_CS_OFF;
    ST7789_DC_ON; //写数据
    SPI_ReadWrite(SPI1, data);
    ST7789_CS_ON;
}

void st7789_write_data(uint16_t data)
{
    ST7789_CS_OFF;
    ST7789_DC_ON; //写数据
    SPI_ReadWrite(SPI1, data >> 8);
    SPI_ReadWrite(SPI1, data & 0xff);
    ST7789_CS_ON;
}

void st7789_write_reg(uint8_t data)
{
    ST7789_CS_OFF;
    ST7789_DC_OFF; //写命令
    SPI_ReadWrite(SPI1, data);
    ST7789_CS_ON;
}

/******************************************************************************
函数说明：设置起始和结束地址
入口数据：x1,x2 设置列的起始和结束地址
        y1,y2 设置行的起始和结束地址
返回值：  无
******************************************************************************/
void st7789_set_addr(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
	//先切换低速SPI
	TFT_SPI0_Init(8,SPI_CLKDIV_2);
	
//		y1+=0x23;
//	  y2+=0x23;
    st7789_write_reg(0x2a); //列地址设置
    st7789_write_data(x1);
    st7789_write_data(x2);
    st7789_write_reg(0x2b); //行地址设置
    st7789_write_data(y1);
    st7789_write_data(y2);
    st7789_write_reg(0x2c); //使能写入数据到RAM
	
	//切换高速SPI
	TFT_SPI0_Init(16,SPI_CLKDIV_2);
}

void st7789_fill(uint16_t xsta, uint16_t ysta, uint16_t xend, uint16_t yend, uint16_t color)
{
    st7789_set_addr(xsta, ysta, xend - 1, yend - 1); //设置光标位置
    ST7789_CS_OFF;
    ST7789_DC_ON; //写数据
	
//		__disable_irq();
	
    for (uint32_t i = ysta; i < yend; i++)
    {
        for (uint32_t j = xsta; j < xend; j++)
        {
            // st7789_write_data(color);
            SPI_ReadWrite(SPI1, color >> 8);
            SPI_ReadWrite(SPI1, color );
        }
    }
		
//		__enable_irq();
		
    ST7789_CS_ON;
}

/******************************************************************************
函数说明：st7789初始化函数
入口数据：无
返回值：  无
******************************************************************************/
void st7789_init(void)
{		
	
	//SPI初始化	
	SPI_InitStructure SPI_initStruct;
	GPIO_Init(ST7789_DC_GPIO, ST7789_DC_PIN, 1, 0, 0, 0);
	GPIO_Init(ST7789_CS_GPIO, ST7789_CS_PIN, 1, 0, 0, 0);
	GPIO_SetBit(ST7789_CS_GPIO, ST7789_CS_PIN); //初始拉高(便于SPI模式下使用)

	PORT_Init(PORTN, PIN0, PORTN_PIN0_SPI1_SCLK, 0);
	PORT_Init(PORTN, PIN5, PORTN_PIN5_SPI1_MOSI, 0);
	
	SPI_initStruct.clkDiv = SPI_CLKDIV_2;
	SPI_initStruct.FrameFormat = SPI_FORMAT_SPI;
	SPI_initStruct.SampleEdge = SPI_FIRST_EDGE;
	SPI_initStruct.IdleLevel = SPI_LOW_LEVEL;
	SPI_initStruct.WordSize = 8;
	SPI_initStruct.Master = 1;
	SPI_initStruct.RXThreshold = 0;
	SPI_initStruct.RXThresholdIEn = 0;
	SPI_initStruct.TXThreshold = 0;
	SPI_initStruct.TXThresholdIEn = 0;
	SPI_initStruct.TXCompleteIEn  = 0;
	SPI_Init(SPI1, &SPI_initStruct);
	SPI_Open(SPI1);
	
	
//	GPIO_Init(GPIOM, PIN5, 1, 0, 0, 0);		//屏幕背光
//	GPIO_SetBit(GPIOM, PIN5);
	
	//辅助IO控制
//  GPIO_Init(ST7789_CS_GPIO, ST7789_CS_PIN, 1, 0, 1, 0);
//    GPIO_Init(ST7789_DC_GPIO, ST7789_DC_PIN, 1, 0, 1, 0);
    GPIO_Init(ST7789_RES_GPIO, ST7789_RES_PIN, 1, 0, 1, 0);
	GPIO_Init(ST7789_BLK_GPIO, ST7789_BLK_PIN, 1, 0, 0, 0);		//屏幕背光
	ST7789_BLK_OFF;
    ST7789_CS_ON;
    ST7789_DC_ON;
    ST7789_RES_ON;
	
	//ST7789_BLK_ON;
	//7789初始化
    delay_ms(1);
	
    ST7789_RES_OFF;
    delay_ms(1);
    ST7789_RES_ON;
    delay_ms(1);



////************* Start Initial Sequence **********//
//    st7789_write_reg(0x11); // Sleep out
//    delay_ms(120);     // Delay 120ms

//    st7789_write_reg(0xCF);
//    st7789_write_data8(0x00);
//    st7789_write_data8(0xD9);
//    st7789_write_data8(0X30);

//    st7789_write_reg(0xED);
//    st7789_write_data8(0x64);
//    st7789_write_data8(0x03);
//    st7789_write_data8(0X12);
//    st7789_write_data8(0X81);

//    st7789_write_reg(0xE8);
//    st7789_write_data8(0x85);
//    st7789_write_data8(0x10);
//    st7789_write_data8(0x78);

//    st7789_write_reg(0xCB);
//    st7789_write_data8(0x39);
//    st7789_write_data8(0x2C);
//    st7789_write_data8(0x00);
//    st7789_write_data8(0x34);
//    st7789_write_data8(0x02);

//    st7789_write_reg(0xF7);
//    st7789_write_data8(0x20);

//    st7789_write_reg(0xEA);
//    st7789_write_data8(0x00);
//    st7789_write_data8(0x00);

//    st7789_write_reg(0xC0);   // Power control
//    st7789_write_data8(0x21); // VRH[5:0]

//    st7789_write_reg(0xC1);   // Power control
//    st7789_write_data8(0x12); // SAP[2:0];BT[3:0]

//    st7789_write_reg(0xC5); // VCM control
//    st7789_write_data8(0x32);
//    st7789_write_data8(0x3C);

//    st7789_write_reg(0xC7); // VCM control2
//    st7789_write_data8(0XC1);

//    st7789_write_reg(0x36); // Memory Access Control
//    st7789_write_data8(0x68); //direction0
//    // st7789_write_data8(0xC8); //direction1
//    // st7789_write_data8(0x78); //direction2
//    // st7789_write_data8(0xA8); //direction3

//    st7789_write_reg(0x3A);
//    st7789_write_data8(0x55);

//    st7789_write_reg(0xB1);
//    st7789_write_data8(0x00);
//    st7789_write_data8(0x18);

//    st7789_write_reg(0xB6); // Display Function Control
//    st7789_write_data8(0x0A);
//    st7789_write_data8(0xA2);

//    st7789_write_reg(0xF2); // 3Gamma Function Disable
//    st7789_write_data8(0x00);

//    st7789_write_reg(0x26); // Gamma curve selected
//    st7789_write_data8(0x01);

//    st7789_write_reg(0xE0); // Set Gamma
//    st7789_write_data8(0x0F);
//    st7789_write_data8(0x20);
//    st7789_write_data8(0x1E);
//    st7789_write_data8(0x09);
//    st7789_write_data8(0x12);
//    st7789_write_data8(0x0B);
//    st7789_write_data8(0x50);
//    st7789_write_data8(0XBA);
//    st7789_write_data8(0x44);
//    st7789_write_data8(0x09);
//    st7789_write_data8(0x14);
//    st7789_write_data8(0x05);
//    st7789_write_data8(0x23);
//    st7789_write_data8(0x21);
//    st7789_write_data8(0x00);

//    st7789_write_reg(0XE1); // Set Gamma
//    st7789_write_data8(0x00);
//    st7789_write_data8(0x19);
//    st7789_write_data8(0x19);
//    st7789_write_data8(0x00);
//    st7789_write_data8(0x12);
//    st7789_write_data8(0x07);
//    st7789_write_data8(0x2D);
//    st7789_write_data8(0x28);
//    st7789_write_data8(0x3F);
//    st7789_write_data8(0x02);
//    st7789_write_data8(0x0A);
//    st7789_write_data8(0x08);
//    st7789_write_data8(0x25);
//    st7789_write_data8(0x2D);
//    st7789_write_data8(0x0F);
//    st7789_write_reg(0x29); // Display on

			st7789_write_reg(0xF0);			
	st7789_write_data8(0x08);
	
	st7789_write_reg(0xF2);			
	st7789_write_data8(0x08);
	
	st7789_write_reg(0x9B);			
	st7789_write_data8(0x51);
	
	st7789_write_reg(0x86);			
	st7789_write_data8(0x53);
	
	st7789_write_reg(0xF2);			
	st7789_write_data8(0x80);
	
	st7789_write_reg(0xF0);			
    st7789_write_data8(0x00);
	
 
  st7789_write_reg(0xF0);      
  st7789_write_data8(0x01);      
 
  st7789_write_reg(0xF1);     
  st7789_write_data8(0x01);      
 
  st7789_write_reg(0xB0);      
  st7789_write_data8(0x54);      

  st7789_write_reg(0xB1);     
  st7789_write_data8(0x3F);      

  st7789_write_reg(0xB2);     
  st7789_write_data8(0x2A);      

  st7789_write_reg(0xB4);     
  st7789_write_data8(0x46);      
 
  st7789_write_reg(0xB5);     
  st7789_write_data8(0x34);      

  st7789_write_reg(0xB6);   
  st7789_write_data8(0xD5);      
 
  st7789_write_reg(0xB7);      
  st7789_write_data8(0x30);      
  
  st7789_write_reg(0xBA);     
  st7789_write_data8(0x00);      
 
  st7789_write_reg(0xBB);     
  st7789_write_data8(0x08);      
  
  st7789_write_reg(0xBC);     
  st7789_write_data8(0x08);      

  st7789_write_reg(0xBD);   
  st7789_write_data8(0x00);      
 
  st7789_write_reg(0xC0);     
  st7789_write_data8(0x80);      
 
  st7789_write_reg(0xC1);     
  st7789_write_data8(0x10);      
 
  st7789_write_reg(0xC2);     
  st7789_write_data8(0x37);      
 
  st7789_write_reg(0xC3);     
  st7789_write_data8(0x80);      
 
  st7789_write_reg(0xC4);      
  st7789_write_data8(0x10);      
 
  st7789_write_reg(0xC5);      
  st7789_write_data8(0x37);      
  
  st7789_write_reg(0xC6);      
  st7789_write_data8(0xA9);      
 
  st7789_write_reg(0xC7);      
  st7789_write_data8(0x41);      
 
  st7789_write_reg(0xC8);     
  st7789_write_data8(0x51);      

  st7789_write_reg(0xC9);     
  st7789_write_data8(0xA9);      
  
  st7789_write_reg(0xCA);    
  st7789_write_data8(0x41);      
 
  st7789_write_reg(0xCB);      
  st7789_write_data8(0x51);      

  st7789_write_reg(0xD0);      
  st7789_write_data8(0x91);      
 
  st7789_write_reg(0xD1);      
  st7789_write_data8(0x68);      
  
  st7789_write_reg(0xD2);      
  st7789_write_data8(0x69);      
  
  st7789_write_reg(0xF5);    
  st7789_write_data8(0x00);
  st7789_write_data8(0xA5);
 
  st7789_write_reg(0xDD);      
  st7789_write_data8(0x3C);      
 
  st7789_write_reg(0xDE);     
  st7789_write_data8(0x3C);      
 
  st7789_write_reg(0xF1);     
  st7789_write_data8(0x10);      
 
  st7789_write_reg(0xF0);      
  st7789_write_data8(0x00);      
 
  st7789_write_reg(0xF0);      
  st7789_write_data8(0x02);      

  st7789_write_reg(0xE0);     
  st7789_write_data8(0x70);      
  st7789_write_data8(0x09);      
  st7789_write_data8(0x0E);     
  st7789_write_data8(0x0A);      
  st7789_write_data8(0x09);     
  st7789_write_data8(0x16);     
  st7789_write_data8(0x37);      
  st7789_write_data8(0x44);    
  st7789_write_data8(0x4D);      
  st7789_write_data8(0x18);     
  st7789_write_data8(0x15);   
  st7789_write_data8(0x15);     
  st7789_write_data8(0x2B);     
  st7789_write_data8(0x2F);      

  st7789_write_reg(0xE1);     
  st7789_write_data8(0x70);      
  st7789_write_data8(0x08);      
  st7789_write_data8(0x0E);     
  st7789_write_data8(0x09);     
  st7789_write_data8(0x09);     
  st7789_write_data8(0x16);      
  st7789_write_data8(0x36);      
  st7789_write_data8(0x43);      
  st7789_write_data8(0x4D);     
  st7789_write_data8(0x17);      
  st7789_write_data8(0x14);     
  st7789_write_data8(0x15);     
  st7789_write_data8(0x2B);      
  st7789_write_data8(0x2F);      

  st7789_write_reg(0xF0);      
  st7789_write_data8(0x10);      
 
  st7789_write_reg(0xF3);    
  st7789_write_data8(0x10);      
  
  st7789_write_reg(0xE0);      
  st7789_write_data8(0x0A);      
  
st7789_write_reg(0xE1);     
st7789_write_data8(0x00);     

st7789_write_reg(0xE2);     
st7789_write_data8(0x00);      

st7789_write_reg(0xE3);      
st7789_write_data8(0x00);      

st7789_write_reg(0xE4);    
st7789_write_data8(0xE0);      

st7789_write_reg(0xE5);    
st7789_write_data8(0x06);      

st7789_write_reg(0xE6);      
st7789_write_data8(0x21);      

st7789_write_reg(0xE7);     
st7789_write_data8(0x00);      

st7789_write_reg(0xE8);     
st7789_write_data8(0x05);      
 
st7789_write_reg(0xE9);      
st7789_write_data8(0x82);      

st7789_write_reg(0xEA);    
st7789_write_data8(0xDF);      
 
st7789_write_reg(0xEB);     
st7789_write_data8(0x89);      

st7789_write_reg(0xEC);    
st7789_write_data8(0x20);      
 
st7789_write_reg(0xED);      
st7789_write_data8(0x14);      
 
st7789_write_reg(0xEE);    
st7789_write_data8(0xFF);      
 
st7789_write_reg(0xEF);     
st7789_write_data8(0x00);      
 
st7789_write_reg(0xF8);      
st7789_write_data8(0xFF);      
  
st7789_write_reg(0xF9);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0xFA);     
st7789_write_data8(0x00);      
 
st7789_write_reg(0xFB);      
st7789_write_data8(0x30);      
 
st7789_write_reg(0xFC);     
st7789_write_data8(0x00);      
 
st7789_write_reg(0xFD);     
st7789_write_data8(0x00);      
 
st7789_write_reg(0xFE);     
st7789_write_data8(0x00);      
 
st7789_write_reg(0xFF);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0x60);     
st7789_write_data8(0x42);      
  
st7789_write_reg(0x61);      
st7789_write_data8(0xE0);      
 
st7789_write_reg(0x62);     
st7789_write_data8(0x40);      
 
st7789_write_reg(0x63);      
st7789_write_data8(0x40);      
 
st7789_write_reg(0x64);      
st7789_write_data8(0x02);      
 
st7789_write_reg(0x65);     
st7789_write_data8(0x00);      

st7789_write_reg(0x66);      
st7789_write_data8(0x40);      
 
st7789_write_reg(0x67);     
st7789_write_data8(0x03);      

st7789_write_reg(0x68);     
st7789_write_data8(0x00);      

st7789_write_reg(0x69);     
st7789_write_data8(0x00);      
 
st7789_write_reg(0x6A);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0x6B);     
st7789_write_data8(0x00);      

st7789_write_reg(0x70);      
st7789_write_data8(0x42);      
 
st7789_write_reg(0x71);      
st7789_write_data8(0xE0);      
 
st7789_write_reg(0x72);    
st7789_write_data8(0x40);      
 
st7789_write_reg(0x73);     
st7789_write_data8(0x40);      
  
st7789_write_reg(0x74);    
st7789_write_data8(0x02);      

st7789_write_reg(0x75);    
st7789_write_data8(0x00);      
 
st7789_write_reg(0x76);      
st7789_write_data8(0x40);      
  
st7789_write_reg(0x77);      
st7789_write_data8(0x03);      
 
st7789_write_reg(0x78);      
st7789_write_data8(0x00);      

st7789_write_reg(0x79);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0x7A);     
st7789_write_data8(0x00);      
  
st7789_write_reg(0x7B);     
st7789_write_data8(0x00);      
 
st7789_write_reg(0x80);     
st7789_write_data8(0x48);      
 
st7789_write_reg(0x81);     
st7789_write_data8(0x00);      
 
st7789_write_reg(0x82);
st7789_write_data8(0x05);      
 
st7789_write_reg(0x83);      
st7789_write_data8(0x02);      
 
st7789_write_reg(0x84);     
st7789_write_data8(0xDD);      
 
st7789_write_reg(0x85);     
st7789_write_data8(0x00);      
 
st7789_write_reg(0x86);     
st7789_write_data8(0x00);      
 
st7789_write_reg(0x87);      
st7789_write_data8(0x00);      
  
st7789_write_reg(0x88);     
st7789_write_data8(0x48);      
 
st7789_write_reg(0x89);      
st7789_write_data8(0x00);      
  
st7789_write_reg(0x8A);      
st7789_write_data8(0x07);      
  
st7789_write_reg(0x8B);      
st7789_write_data8(0x02);      

st7789_write_reg(0x8C);     
st7789_write_data8(0xDF);      
  
st7789_write_reg(0x8D);     
st7789_write_data8(0x00);      

st7789_write_reg(0x8E);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0x8F);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0x90);     
st7789_write_data8(0x48);      
  
st7789_write_reg(0x91);     
st7789_write_data8(0x00);     

st7789_write_reg(0x92);     
st7789_write_data8(0x09);      
 
st7789_write_reg(0x93);      
st7789_write_data8(0x02);     
 
st7789_write_reg(0x94);      
st7789_write_data8(0xE1);      
 
st7789_write_reg(0x95);      
st7789_write_data8(0x00);     

st7789_write_reg(0x96);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0x97);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0x98);     
st7789_write_data8(0x48);     
 
st7789_write_reg(0x99);      
st7789_write_data8(0x00);      
  
st7789_write_reg(0x9A);      
st7789_write_data8(0x0B);      
 
st7789_write_reg(0x9B);      
st7789_write_data8(0x02);      
 
st7789_write_reg(0x9C);      
st7789_write_data8(0xE3);      
 
st7789_write_reg(0x9D);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0x9E);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0x9F);      
st7789_write_data8(0x00);      
  
st7789_write_reg(0xA0);      
st7789_write_data8(0x48);      
 
st7789_write_reg(0xA1);      
st7789_write_data8(0x00);      
  
st7789_write_reg(0xA2);      
st7789_write_data8(0x04);      
 
st7789_write_reg(0xA3);      
st7789_write_data8(0x02);      

st7789_write_reg(0xA4);      
st7789_write_data8(0xDC);      
 
st7789_write_reg(0xA5);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0xA6);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0xA7);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0xA8);      
st7789_write_data8(0x48);      
  
st7789_write_reg(0xA9);      
st7789_write_data8(0x00);      
  
st7789_write_reg(0xAA);      
st7789_write_data8(0x06);      

st7789_write_reg(0xAB);      
st7789_write_data8(0x02);      
  
st7789_write_reg(0xAC);      
st7789_write_data8(0xDE);      
 
st7789_write_reg(0xAD);      
st7789_write_data8(0x00);      
  
st7789_write_reg(0xAE);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0xAF);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0xB0);      
st7789_write_data8(0x48);      

st7789_write_reg(0xB1);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0xB2);      
st7789_write_data8(0x08);      
  
st7789_write_reg(0xB3);      
st7789_write_data8(0x02);      

st7789_write_reg(0xB4);      
st7789_write_data8(0xE0);      
 
st7789_write_reg(0xB5);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0xB6);      
st7789_write_data8(0x00);      

st7789_write_reg(0xB7);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0xB8);      
st7789_write_data8(0x48);      

st7789_write_reg(0xB9);      
st7789_write_data8(0x00);      

st7789_write_reg(0xBA);      
st7789_write_data8(0x0A);      
 
st7789_write_reg(0xBB);      
st7789_write_data8(0x02);      
  
st7789_write_reg(0xBC);      
st7789_write_data8(0xE2);      

st7789_write_reg(0xBD);      
st7789_write_data8(0x00);      

st7789_write_reg(0xBE);      
st7789_write_data8(0x00);      

st7789_write_reg(0xBF);      
st7789_write_data8(0x00);      

st7789_write_reg(0xC0);      
st7789_write_data8(0x12);      
 
st7789_write_reg(0xC1);      
st7789_write_data8(0xAA);      
 
st7789_write_reg(0xC2);      
st7789_write_data8(0x65);      
 
st7789_write_reg(0xC3);      
st7789_write_data8(0x74);      
 
st7789_write_reg(0xC4);      
st7789_write_data8(0x47);      

st7789_write_reg(0xC5);      
st7789_write_data8(0x56);      
 
st7789_write_reg(0xC6);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0xC7);      
st7789_write_data8(0x88);      
 
st7789_write_reg(0xC8);      
st7789_write_data8(0x99);      
 
st7789_write_reg(0xC9);      
st7789_write_data8(0x33);      
 
st7789_write_reg(0xD0);      
st7789_write_data8(0x21);      
 
st7789_write_reg(0xD1);      
st7789_write_data8(0xAA);      
 
st7789_write_reg(0xD2);      
st7789_write_data8(0x65);      

st7789_write_reg(0xD3);      
st7789_write_data8(0x74);      

st7789_write_reg(0xD4);      
st7789_write_data8(0x47);      
 
st7789_write_reg(0xD5);      
st7789_write_data8(0x56);      
 
st7789_write_reg(0xD6);      
st7789_write_data8(0x00);      
  
st7789_write_reg(0xD7);      
st7789_write_data8(0x88);      
 
st7789_write_reg(0xD8);      
st7789_write_data8(0x99);      
 
st7789_write_reg(0xD9);      
st7789_write_data8(0x33);      

st7789_write_reg(0xF3);      
st7789_write_data8(0x01);      
 
st7789_write_reg(0xF0);      
st7789_write_data8(0x00);      

st7789_write_reg(0xF0);      
st7789_write_data8(0x01);      
 
st7789_write_reg(0xF1);      
st7789_write_data8(0x01);      
 
st7789_write_reg(0xA0);      
st7789_write_data8(0x0B);      
 
st7789_write_reg(0xA3);      
st7789_write_data8(0x2A);      
 
st7789_write_reg(0xA5);      
st7789_write_data8(0xC3);      

delay_ms(1);  

st7789_write_reg(0xA3);      
st7789_write_data8(0x2B);      

st7789_write_reg(0xA5);      
st7789_write_data8(0xC3);   

delay_ms(1);  
    
st7789_write_reg(0xA3);      
st7789_write_data8(0x2C);      
 
st7789_write_reg(0xA5);      
st7789_write_data8(0xC3);      

delay_ms(1);     

st7789_write_reg(0xA3);      
st7789_write_data8(0x2D);      
  
st7789_write_reg(0xA5);      
st7789_write_data8(0xC3);      

delay_ms(1);      

st7789_write_reg(0xA3);      
st7789_write_data8(0x2E);      
 
st7789_write_reg(0xA5);      
st7789_write_data8(0xC3);      

delay_ms(1);      

st7789_write_reg(0xA3);      
st7789_write_data8(0x2F);      

st7789_write_reg(0xA5);      
st7789_write_data8(0xC3);      

delay_ms(1);      

st7789_write_reg(0xA3);      
st7789_write_data8(0x30);      
 
st7789_write_reg(0xA5);      
st7789_write_data8(0xC3);      

delay_ms(1);      

st7789_write_reg(0xA3);    
st7789_write_data8(0x31);      
 
st7789_write_reg(0xA5);      
st7789_write_data8(0xC3);      

delay_ms(1);      

st7789_write_reg(0xA3);      
st7789_write_data8(0x32);      

st7789_write_reg(0xA5);      
st7789_write_data8(0xC3);      

delay_ms(1);      

st7789_write_reg(0xA3);      
st7789_write_data8(0x33);      
 
st7789_write_reg(0xA5);      

delay_ms(1);      

st7789_write_reg(0xA0);      
st7789_write_data8(0x09);      

st7789_write_reg(0xF1);      
st7789_write_data8(0x10);      

st7789_write_reg(0xF0);      
st7789_write_data8(0x00);      

st7789_write_reg(0x2A);      
st7789_write_data8(0x00);      
st7789_write_data8(0x00);      
st7789_write_data8(0x01);      
st7789_write_data8(0x67);      

st7789_write_reg(0x2B);      
st7789_write_data8(0x01);      
st7789_write_data8(0x68);      
st7789_write_data8(0x01);      
st7789_write_data8(0x68);      
 
st7789_write_reg(0x4D);      
st7789_write_data8(0x00);     
 
st7789_write_reg(0x4E);      
st7789_write_data8(0x00);      
 
st7789_write_reg(0x4F);    
st7789_write_data8(0x00);      

st7789_write_reg(0x4C);     
st7789_write_data8(0x01);      

delay_ms(10);      

st7789_write_reg(0x4C);     
st7789_write_data8(0x00);      
 
st7789_write_reg(0x2A);      
st7789_write_data8(0x00);      
st7789_write_data8(0x00);      
st7789_write_data8(0x01);      
st7789_write_data8(0x67);      

st7789_write_reg(0x2B);      
st7789_write_data8(0x00);      
st7789_write_data8(0x00);      
st7789_write_data8(0x01);      
st7789_write_data8(0x67);    

st7789_write_reg(0x3A);     
st7789_write_data8(0x55);

st7789_write_reg(0x21);      

st7789_write_reg(0x11);      

delay_ms(120);
       
st7789_write_reg(0x29); 
		ST7789_Clear(0Xf800);
}

void ST7789_Clear(uint16_t rgb)
{
//	st7789_fill(0, 0, LCD_W, LCD_H, rgb);
	ST7789_DMA_Clear(rgb);
}


void st7789_drawpoint(uint16_t x, uint16_t y, uint16_t color)
{
    st7789_set_addr(x, y, x, y); //设置光标位置
    st7789_write_data(color);
}



void TFT_SPI0_Init(uint16_t WORDSIZE,uint16_t CLK)
{
  SPI_InitStructure SPI_initStruct;
	
	SPI_initStruct.clkDiv = CLK;
	SPI_initStruct.FrameFormat = SPI_FORMAT_SPI;
	SPI_initStruct.SampleEdge = SPI_FIRST_EDGE;
	SPI_initStruct.IdleLevel = SPI_LOW_LEVEL;
	SPI_initStruct.WordSize = WORDSIZE;
	SPI_initStruct.Master = 1;
	SPI_initStruct.RXThreshold = 0;
	SPI_initStruct.RXThresholdIEn = 0;
	SPI_initStruct.TXThreshold = 0;
	SPI_initStruct.TXThresholdIEn = 0;
	SPI_initStruct.TXCompleteIEn  = 0;
	
	SPI_Init(SPI1, &SPI_initStruct);
	SPI_Open(SPI1);	
}	


/****************************************************************************************************************************************** 
* 函数名称: mpu_lcd_dmaclear()
* 功能说明: 
* 输    入: 
* 输    出: 
* 注意事项: 
******************************************************************************************************************************************/
//void ST7789_DMA_Clear(uint32_t *data)
void ST7789_DMA_Clear(uint16_t color)
{	
	DMA_InitStructure DMA_initStruct;
	
	st7789_set_addr(0, 0, SPI_VPIX-1,  SPI_HPIX-1);

	ST7789_CS_OFF;
	ST7789_DC_ON; //写数据
	
	SPI1->CTRL &= ~SPI_CTRL_DMATXEN_Msk;
	SPI1->CTRL |= (1 << SPI_CTRL_DMATXEN_Pos);
	DMA_initStruct.Mode = DMA_MODE_SINGLE;
	DMA_initStruct.Unit = DMA_UNIT_HALFWORD;
	DMA_initStruct.Count = SPI_VPIX * SPI_HPIX;
	DMA_initStruct.SrcAddr = (uint32_t)&color;
	DMA_initStruct.SrcAddrInc = 0;
	DMA_initStruct.DstAddr = (uint32_t)&SPI1->DATA;
	DMA_initStruct.DstAddrInc = 0;
	DMA_initStruct.Handshake = DMA_CH1_SPI1TX;
	DMA_initStruct.Priority = DMA_PRI_LOW;
	DMA_initStruct.INTEn = 0;
	DMA_CH_Init(DMA_CH1, &DMA_initStruct);
	DMA_CH_Open(DMA_CH1);
	
	while(ST7789_Done() == 0);
	
	ST7789_CS_ON;
}


//void ST7789_DMA_PIC(uint16_t color)
//{	
//	DMA_InitStructure DMA_initStruct;
//	
//	st7789_set_addr(0, 0, SPI_VPIX-1,  SPI_HPIX-1);

//	ST7789_CS_OFF;
//	ST7789_DC_ON; //写数据
//	
//	SPI1->CTRL &= ~SPI_CTRL_DMATXEN_Msk;
//	SPI1->CTRL |= (1 << SPI_CTRL_DMATXEN_Pos);
//	DMA_initStruct.Mode = DMA_MODE_SINGLE;
//	DMA_initStruct.Unit = DMA_UNIT_HALFWORD;
//	DMA_initStruct.Count = SPI_VPIX * SPI_HPIX;
//	DMA_initStruct.SrcAddr = (uint32_t)&color;
//	DMA_initStruct.SrcAddrInc = 0;
//	DMA_initStruct.DstAddr = (uint32_t)&SPI1->DATA;
//	DMA_initStruct.DstAddrInc = 0;
//	DMA_initStruct.Handshake = DMA_CH1_SPI1TX;
//	DMA_initStruct.Priority = DMA_PRI_LOW;
//	DMA_initStruct.INTEn = 0;
//	DMA_CH_Init(DMA_CH1, &DMA_initStruct);
//	DMA_CH_Open(DMA_CH1);
//	
//	while(ST7789_Done() == 0);
//	
//	ST7789_CS_ON;
//}

uint32_t ST7789_Done(void)
{
	if(DMA_CH_INTStat(DMA_CH1, DMA_IT_DONE))
	{
		DMA_CH_INTClr(DMA_CH1, DMA_IT_DONE);
		
		return 1;
	}
	else
	{
		return 0;
	}
}
uint32_t FlagTestSpi = 0;
uint32_t tickstart2=0;
uint32_t tickend2=0;
void SPI_TFT_Test(void)
{
	while(1)
	{
		tickstart2 = swm_gettick();
		for(uint8_t i=0;i<120;i++)
		ST7789_DMA_Clear(0xF100);
		tickend2=swm_gettick()-tickstart2;
//		while(ST7789_Done() == 0);
//		swm_delay_ms(2000);
//		for(uint32_t i = 0; i < 4000 * 10000; i++);
//		FlagTestSpi++;
		
		ST7789_DMA_Clear(0x07B0);
//		while(ST7789_Done() == 0);
//		swm_delay_ms(2000);
//		for(uint32_t i = 0; i < 4000 * 10000; i++);
		
//		FlagTestSpi++;
	}
}


extern volatile uint8_t Flag_DMA_IRQ_Send;
extern volatile uint8_t Flag_DMA_IRQ_Get;

void SPI_TFT_Display(uint16_t* data, uint32_t halfword_num)
{
		//辅助标志	
		Flag_DMA_IRQ_Send = 0;
	
		DMA_InitStructure DMA_initStruct;


		SPI1->CTRL &= ~SPI_CTRL_DMATXEN_Msk;
		SPI1->CTRL |= (1 << SPI_CTRL_DMATXEN_Pos);
		DMA_initStruct.Mode = DMA_MODE_SINGLE;
		DMA_initStruct.Unit = DMA_UNIT_HALFWORD;
		DMA_initStruct.Count = halfword_num;
		DMA_initStruct.SrcAddr = (uint32_t)data;
		DMA_initStruct.SrcAddrInc = 1;
		DMA_initStruct.DstAddr = (uint32_t)&SPI1->DATA;
		DMA_initStruct.DstAddrInc = 0;
		DMA_initStruct.Handshake = DMA_CH1_SPI1TX;
		DMA_initStruct.Priority = DMA_PRI_LOW;
		DMA_initStruct.INTEn = 1;
		DMA_CH_Init(DMA_CH1, &DMA_initStruct);
		DMA_CH_Open(DMA_CH1);
	
//		while(ST7789_Done() == 0);


		

}
